package com.titaniume.java;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import  com.titaniume.java.Employee.Status;

/**
 *@program: java8
 *@description:
 *@author: renfei.huang
 *@create: 2021-10-29 15:59
 */
public class TestStreamAPI3 {


    List<Employee> emps = Arrays.asList(
            new Employee(102, "李四", 79, 6666.66, Status.BUSY),
            new Employee(101, "张三", 18, 9999.99, Status.FREE),
            new Employee(103, "王五", 28, 3333.33, Status.VOCATION),
            new Employee(104, "赵六", 8, 7777.77, Status.BUSY),
            new Employee(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee(105, "田七", 38, 5555.55, Status.BUSY)
    );

  /*
     查找与匹配
     all-Math  检查是否匹配所有元素
     any-Math 检查是否至少匹配一个元素
     noneMath 检查是否没有匹配所有元素
     findFirst 返回第一个元素
     findAny 返回流流中的任意元素
     count 返回流中的总个数
     max 返回流总最大值
     min 返回流中最小值
   */

    @Test
    public void test2(){
        long count = emps.stream()
                .count();
        System.out.println(count);


        Optional<Employee> op1 = emps.stream()
                .max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(op1.get());


        Optional<Double> op2 = emps.stream()
                .map(Employee::getSalary)
                .min(Double::compare);
        System.out.println(op2.get());


    }

    @Test
    public void test(){

        boolean b1 = emps.stream().
                allMatch((e) -> e.getStatus().equals(Status.BUSY));
        System.out.println(b1);

        boolean b2 = emps.stream().
                anyMatch((e) -> e.getStatus().equals(Status.BUSY));
        System.out.println(b2);


        boolean b3 = emps.stream().
                noneMatch((e) -> e.getStatus().equals(Status.BUSY));
        System.out.println(b3);


        Optional<Employee> op1 = emps.stream()
                .sorted((e1, e2) -> -Double.compare(e1.getSalary(), e2.getSalary()))
                .findFirst();
        System.out.println(op1.get());


        Optional<Employee> op2 = emps.stream()
                .filter((e) -> e.getStatus().equals(Status.BUSY))
                .findAny();
        System.out.println(op2.get());



    }



}
